﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Windows.Forms;

namespace HTF_HMI
{
    public partial class AUDIT_DEVICE_EDIT : Form
    {      
        public bool isAdd = true;
        public string reportId = string.Empty;        
        private AUDIT_DEVICE audit_device;

        public AUDIT_DEVICE_EDIT(AUDIT_DEVICE act_form,string id)
        {
            InitializeComponent();
            audit_device = act_form;
            BindArea();
            BindPosition();
            if (!string.IsNullOrEmpty(id))
            {
                reportId = id;
                isAdd = false;
                BindDevice(reportId);
                txt_code.Enabled = false;             
            }           
        }

        private void BindArea()
        {
            var area = DBClass.DBHelper.GetAuditArea();
            cmb_area.DisplayMember = "AREA_NAME";
            cmb_area.ValueMember = "AREA_ID";
            cmb_area.DataSource = area;
        }

        private void BindPosition()
        {
            var type = DBClass.DBHelper.GetAuditDeviceType();
            cmb_position.DisplayMember = "DEVICE_TYPE_NAME";
            cmb_position.ValueMember = "DEVICE_TYPE_NAME";
            cmb_position.DataSource = type;
        }

        private void BindDevice(string id)
        {   
            var task = DBClass.DBHelper.GetAuditDeviceByDeviceID(id).FirstOrDefault();
            if (task!=null)
            {
                txt_name.Text = task.DEVICE_NAME.ToString();
                txt_code.Text = task.DEVICE_ID.ToString();
                cmb_area.Text = task.AREA_NAME;
                cmb_position.Text = task.DEVICE_TYPE_NAME;
            }
        }       

        private void Number_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsNumber(e.KeyChar) || e.KeyChar == (char)8 || e.KeyChar == '\b')
            {
                e.Handled = true;
            }
        }

        private void btn_cancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void btn_save_Click(object sender, EventArgs e)
        { 
            string strArea = cmb_area.Text;
            string strCode = txt_code.Text.Trim();
            string strName = txt_name.Text.Trim();
            string strPosition = cmb_position.Text;

            if (string.IsNullOrEmpty(strArea))
            {
                MessageBox.Show("请选择巡检点位区域！");
                return;
            }
            if (string.IsNullOrEmpty(strCode))
            {
                MessageBox.Show("请输入巡检点位编码！");
                return;
            }
            if (string.IsNullOrEmpty(strName))
            {
                MessageBox.Show("请输入巡检点位名称！");
                return;
            }
            if (string.IsNullOrEmpty(strPosition))
            {
                MessageBox.Show("请选择巡检点位位置！");
                return;
            }

            try
            {
                string sql = string.Empty;              
                DBEntity.Models.AUDIT_DEVICE device = new DBEntity.Models.AUDIT_DEVICE();
                if (!isAdd)
                {
                    device.DEVICE_ID = txt_code.Text;
                    device.DEVICE_NAME = strName;
                    device.AREA_NAME = strArea;
                    device.DEVICE_TYPE_NAME = strPosition;                   

                    bool result = DBClass.DBHelper.UpdateAuditDevice(device);
                    if (result )
                    {    
                        MessageBox.Show("保存成功！");
                        this.DialogResult = DialogResult.OK;

                        audit_device.btn_query_Click(null, null);
                    }
                }
                else
                {
                    device.DEVICE_ID = txt_code.Text;
                    device.DEVICE_NAME = strName;
                    device.AREA_NAME = strArea;
                    device.DEVICE_TYPE_NAME = strPosition;
                    if (DBClass.DBHelper.GetAuditDeviceByDeviceID(txt_code.Text).Count() > 0)
                    {
                        MessageBox.Show("保存失败！已存在相同编码的巡检点位！");
                        this.DialogResult = DialogResult.OK;
                    }
                    else
                    {
                        DBEntity.Models.AUDIT_DEVICE newDevice = DBClass.DBHelper.InsertAuditDevice(device);
                        if (newDevice != null)
                        {
                            MessageBox.Show("保存成功！");
                            this.DialogResult = DialogResult.OK;

                            audit_device.btn_query_Click(null, null);
                        }
                        else
                        {
                            MessageBox.Show("保存失败！");                           
                        }
                    }                    
                }               
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                DBClass.Log.InsertLog("保存巡检点位失败！"+ex.Message);  
            }
        }
    }
}
